package tests;

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

import tripleheap.InvalidSlotNumberException;
import tripleheap.InvalidTripleSizeException;
import tripleheap.THFBufMgrException;
import tripleheap.THFDiskMgrException;
import tripleheap.THFException;

import diskmgr.PCounter;
import diskmgr.Stream;
import basicpattern.BpJoin;
import global.AttrType;
import global.BPOrder;
import global.RID;
import global.SystemDefs;
import heap.HFBufMgrException;
import heap.HFDiskMgrException;
import heap.HFException;
import heap.Heapfile;
import heap.Scan;
import heap.Tuple;
import iterator.*;

/*
 * 
 * KUTTIDB urs knows 0 0 3 0 0 type 0 0 {1:3} 0 1 1 0 urs 0 0 0 {1:2:3} 0 1 1 1 100 100
)
 */

public class BPJoinTest {
	public static void main(String[] args) throws InvalidSlotNumberException,
			InvalidTripleSizeException, THFException, THFDiskMgrException,
			THFBufMgrException, Exception {
		String rdfdbname = args[0];
		SystemDefs.MINIBASE_DBNAME = rdfdbname;
		String SF1 = args[1];
		String PF1 = args[2];
		String OF1 = args[3];
		double CF1 = Double.parseDouble(args[4]);
		int iJNP = Integer.parseInt(args[5]);
		int iJONO = Integer.parseInt(args[6]);
		String iRSF = args[7];
		String iRPF = args[8];
		String iROF = args[9];
		double iRCF = Double.parseDouble(args[10]);
		String iLONP = args[11];
		int iORS = Integer.parseInt(args[12]);
		int iORO = Integer.parseInt(args[13]);
		int oJNP = Integer.parseInt(args[14]);
		int oJONO = Integer.parseInt(args[15]);
		String oRSF = args[16];
		String oRPF = args[17];
		String oROF = args[18];
		double oRCF = Double.parseDouble(args[19]);
		String oLONP = args[20];
		int oORS = Integer.parseInt(args[21]);
		int oORO = Integer.parseInt(args[22]);
		int SO = Integer.parseInt(args[23]);
		int NP = Integer.parseInt(args[24]);
		int NumPages = Integer.parseInt(args[25]);
		int NumBuf = Integer.parseInt(args[26]);

		int i = 0;
		int count1 = 1,count2 =1;
		for (int x = 0; x < iLONP.length(); x++) {
			if (iLONP.charAt(x) == ':')
				count1++;
		}
		for (int y = 0; y < oLONP.length(); y++) {
			if (oLONP.charAt(y) == ':')
				count2++;
		}
		i = 0;
		int[] iLONP1 = new int[count1];
		Scanner sc = new Scanner(iLONP.substring(1, iLONP.length() - 1))
				.useDelimiter(":");
		while (sc.hasNext()) {
			iLONP1[i] = sc.nextInt();
			i++;
		}
		i = 0;
		int[] oLONP1 = new int[count2];
		sc = new Scanner(oLONP.substring(1, oLONP.length() - 1))
				.useDelimiter(":");
		while (sc.hasNext()) {
			oLONP1[i] = sc.nextInt();
			i++;
		}


		BpJoin bpj2 = null;
		BpJoin bpj1 = null;

		File file = new File(rdfdbname);
		boolean exists = file.exists();
		if (exists) {
			SystemDefs.MINIBASE_RESTART_FLAG = false;
			SystemDefs sysdef = new SystemDefs(rdfdbname, 0, NumBuf, "Clock");
		} else {
			System.out.println("DB does not exits");
		}
		Stream stream = SystemDefs.JavabaseDB.openStream(rdfdbname, 0, SF1,
				PF1, OF1, CF1, NumPages);
		Iterator left_lt = (Iterator) stream;
		BPIterator bpit = (BPIterator) left_lt;
		Heapfile hf = new Heapfile("Madarchod");
		Tuple FTuple, JTuple;

		try {
			bpj2 = new BpJoin(100, 3, bpit, iJNP, iJONO, iRSF, iRPF, iROF,
					iRCF, iLONP1, iORS, iORO);

			bpj1 = new BpJoin(100, 3, (iterator.BPIterator) bpj2, oJNP, oJONO,
					oRSF, oRPF, oROF, oRCF, oLONP1, oORS, oORO);

			int totalNodes = oLONP1.length;
			if (oORS == 1)
				totalNodes++;
			if (oORO == 1)
				totalNodes++;
			totalNodes++;

			AttrType[] attrType = new AttrType[totalNodes];
			for (int l = 0; l < totalNodes - 1; l++)
				attrType[l] = new AttrType(AttrType.attrString);

			attrType[totalNodes - 1] = new AttrType(AttrType.attrReal);

			short[] strSize = new short[totalNodes - 1];
			for (int m = 0; m < totalNodes - 1; m++)
				strSize[m] = 100;

			BPOrder order = new BPOrder(SO);
			BPSort bpSort = new BPSort(attrType, (short) totalNodes, strSize,
					(iterator.BPIterator) bpj1, order, NP, NumPages);
			while (true) {
				FTuple = bpSort.get_next();
				if (FTuple == null)
					break;
				FTuple.setHdr((short) totalNodes, attrType, strSize);

				for (int j = 0; j < totalNodes - 1; j++)
					System.out.print(FTuple.getStrFld(j + 1) + "\t\t");
				float conf = FTuple.getFloFld(totalNodes);
				System.out.println(conf + "\n");
			}
			bpSort.close();
			stream.close();
		} catch (Exception e) {

			System.err.println("*** Error preparing for nested_loop_join");
			System.err.println("" + e);
			Runtime.getRuntime().exit(1);
		}

		System.out.println("No of reads : " + PCounter.rcounter
				+ "\nNo of writes : " + PCounter.wcounter);
		SystemDefs.JavabaseDB.closeDB();
	}
}